﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;

namespace Message
{
    public partial class MessageForm2 : Form
    {
        Image img = Message.Properties.Resources.img;//Image.FromFile("D://img.png");
        Bitmap BackgroundBitmap;
        String titleText;
        String contentText;
        Rectangle TitleRectangle = new Rectangle(10, 10, 15, 15);
        Rectangle TitlebarRectangle = new Rectangle(30, 10, 15, 15);
        Rectangle ContentRectangle = new Rectangle(10, 50, 55, 55);
        Rectangle CloseBtnRectangle = new Rectangle(100, 100, 50, 50);
        int CurrentState, currentTop;

        private const int WM_NCLBUTTONDOWN = 0x00A1;
        private const int HT_CAPTION = 0x002;

        Timer timer1 = new Timer();
        Timer timer2 = new Timer();
        int intTime = 0;//记录time2的值；

        public MessageForm2()
        {
            InitializeComponent();


        }
        private void Form2_Load(object sender, EventArgs e)
        {
        }
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        //发送消息//winuser.h 中有函数原型定义     
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture(); //释放鼠标捕捉winuser.h     
        [DllImportAttribute("user32.dll")] //winuser.h     
        private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
        public void SetBackgroundBitmap(Image image, Color transparencyColor)
        {
            BackgroundBitmap = new Bitmap(image);
            Width = BackgroundBitmap.Width + 16;
            Height = BackgroundBitmap.Height + 38;
            Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
        }
        public Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)
        {
            if (bitmap == null)
                throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");
            int height = bitmap.Height;
            int width = bitmap.Width;
            GraphicsPath path = new GraphicsPath();
            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    if (bitmap.GetPixel(i, j) == transparencyColor)
                        continue;
                    int x0 = i;
                    while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))
                        i++;
                    path.AddRectangle(new Rectangle(x0, j, i - x0, 1));
                }
            }
            Region region = new Region(path);
            path.Dispose();
            return region;
        }
        public void ShowForm(string ftitletext, string fcontenttext, Rectangle fRegionofFormTitle,
        Rectangle fRegionofFormTitlebar, Rectangle fRegionofFormContent, Rectangle fRegionofCloseBtn)
        {
            this.MouseDown += new MouseEventHandler(Form2_MouseDown);
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer2.Tick += new EventHandler(timer2_Tick);
            Color green = new Color();
            green = Color.FromArgb(0, 255, 0);
            SetBackgroundBitmap(img, green );          
            Rectangle WorkAreaRectangle = System.Windows.Forms.Screen.GetWorkingArea(this);
            //Rectangle WorkAreaRectangle = System.Windows.Forms.Screen.GetBounds(this );
            //Rectangle WorkAreaRectangle = System.Windows.Forms.SystemInformation.VirtualScreen;//用这个，form2会在form1的位置闪一闪再在屏幕右下角出现
            titleText = ftitletext;
            contentText = fcontenttext;
            WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
            FormBorderStyle = FormBorderStyle.None;
           // this.Top = WorkAreaRectangle.Height; //+ this.Height;
            WindowState = FormWindowState.Normal;
            this.SetBounds(WorkAreaRectangle.Width - this.Width,
            WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
            CurrentState = 1;
            timer1.Enabled = true;
            TitleRectangle = fRegionofFormTitle;
            TitlebarRectangle = fRegionofFormTitlebar;
            ContentRectangle = fRegionofFormContent;
            CloseBtnRectangle = fRegionofCloseBtn;
            ShowWindow(this.Handle, 4);
        }//#define SW_SHOWNOACTIVATE   
        public void timer1_Tick(object sender,EventArgs e)
        {
            timer1.Interval = 10;
            Rectangle WorkAreaRectangle = System.Windows.Forms.Screen.GetWorkingArea(this);
            if (CurrentState==1)
            {
                if (currentTop < this.Height)
                {
                    currentTop = currentTop + Convert.ToInt16(this.Height / 10);
                    if (currentTop >this.Height)
                    {
                        currentTop = this.Height;
                    }
                    this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
                }
                else
                {
                    CurrentState = 2;
                    timer2.Enabled = true;
                    timer1.Enabled = false;
                } 
            }
            if (CurrentState == 3)
            {
                if (currentTop > 0)
                {
                    currentTop = currentTop - Convert.ToInt16(this.Height / 10);
                    this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
                }
                else
                {
                    timer1.Enabled = false;
                    timer1.Dispose();
                    this.Close();
                    this.Dispose();
                }
            }
        }
        public void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Interval = 10;
            intTime += 1;
            timer1.Enabled = false;

            if (CurrentState == 2&&intTime==200)
            {
                intTime = 0;
                CurrentState = 3;
                timer1.Enabled = true;
                timer2.Enabled = false;
                timer2.Dispose();
            }
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Graphics grfx = e.Graphics;
            grfx.PageUnit = GraphicsUnit.Pixel;
            Graphics offScreenGraphics;
            Bitmap offscreenBitmap;
            offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);
            offScreenGraphics = Graphics.FromImage(offscreenBitmap);
            if (BackgroundBitmap != null)
            {
                offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0,
                BackgroundBitmap.Width, BackgroundBitmap.Height);

                offScreenGraphics.DrawString(contentText, new Font(new FontFamily("黑体"),9,FontStyle.Regular ), Brushes.Black , ContentRectangle);
                offScreenGraphics.DrawString(titleText, new Font(new FontFamily("黑体"), 9, FontStyle.Regular), Brushes.Black, TitleRectangle);
                                            
                
            }
            //DrawText(offScreenGraphics);
            grfx.DrawImage(offscreenBitmap, 0, 0);
        }
        private void Form2_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (TitlebarRectangle.Contains(e.Location)) //单击标题栏时拖动     
                {
                    ReleaseCapture(); //释放鼠标捕捉     
                    SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); //发送左键点击的     
                    //消息至该窗体(标题栏)     
                }
                if (CloseBtnRectangle.Contains(e.Location)) //单击Close按钮关闭     
                {
                    this.Hide();
                    currentTop = 1;
                }
                if (ContentRectangle.Contains(e.Location)) //单击内容区域     
                {
                    System.Diagnostics.Process.Start("http://www.codesky.net/search/all/1809/?page=3");
                }
            }
        }
    }
}
